package com.lhj.library.application.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lhj.library.application.convert.BookDtoConverter;
import com.lhj.library.application.convert.RecordDtoConverter;
import com.lhj.library.application.dto.BookDTO;
import com.lhj.library.application.dto.RecordDTO;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.common.entity.Result;
import com.lhj.library.domain.entity.BookBO;
import com.lhj.library.domain.entity.RecordBO;
import com.lhj.library.domain.service.BookDomainService;
import com.lhj.library.infra.basic.entity.Book;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Resource
    private BookDomainService bookDomainService;

    /**
     * 新增图书
     *
     * @param bookDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody BookDTO bookDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("BookController.add.dto:{}", JSON.toJSONString(bookDTO));
            }
            Preconditions.checkNotNull(bookDTO.getName(), "图书名字不能为空");
            Preconditions.checkNotNull(bookDTO.getAuthor(), "图书作者不能为空");
            Preconditions.checkNotNull(bookDTO.getPublish(), "出版社不能为空");
            Preconditions.checkNotNull(bookDTO.getIsbn(), "图书编号不能为空");
            Preconditions.checkNotNull(bookDTO.getPublishDate(), "出版日期不能为空");
            BookBO bookBO = BookDtoConverter.INSTANCE.convertDtoToBookBo(bookDTO);
            return Result.ok(bookDomainService.add(bookBO));
        } catch (Exception e) {
            return Result.fail("新增图书失败");
        }
    }

    /**
     * 删除图书
     *
     * @param bookDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody BookDTO bookDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("BookController.delete.dto:{}", JSON.toJSONString(bookDTO));
            }
            Preconditions.checkNotNull(bookDTO.getId(), "图书id不能为空");
            BookBO bookBO = BookDtoConverter.INSTANCE.convertDtoToBookBo(bookDTO);
            return Result.ok(bookDomainService.delete(bookBO));
        } catch (Exception e) {
            return Result.fail("删除图书失败");
        }
    }

    /**
     * 更新图书
     *
     * @param bookDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody BookDTO bookDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("BookController.update.dto:{}", JSON.toJSONString(bookDTO));
            }
            Preconditions.checkNotNull(bookDTO.getId(), "图书id不能为空");
            BookBO bookBO = BookDtoConverter.INSTANCE.convertDtoToBookBo(bookDTO);
            return Result.ok(bookDomainService.update(bookBO));
        } catch (Exception e) {
            return Result.fail("更新图书失败");
        }
    }

    /**
     * 查询图书
     *
     * @return
     */
    @PostMapping("/select")
    public Result<BookDTO> select(@RequestBody BookDTO bookDTO) {
        try {

            if (log.isInfoEnabled()) {
                log.info("BookController.select.dto:{}", JSON.toJSONString(bookDTO));
            }
            BookBO bookBO = BookDtoConverter.INSTANCE.convertDtoToBookBo(bookDTO);
            BookBO queryBookBo = bookDomainService.select(bookBO);
            BookDTO queryBookDto = BookDtoConverter.INSTANCE.convertBotoDto(queryBookBo);
            return Result.ok(queryBookDto);
        } catch (Exception e) {
            return Result.fail("查询图书失败");
        }
    }

    /**
     * 查询图书列表
     *
     * @return
     */
    @PostMapping("/selectByPage")
    public Result<PageResult<Book>> selectByPage(@RequestBody BookDTO bookDTO) {
        try {

            if (log.isInfoEnabled()) {
                log.info("BookController.selectByPage.dto:{}", JSON.toJSONString(bookDTO));
            }
            BookBO bookBO = BookDtoConverter.INSTANCE.convertDtoToBookBo(bookDTO);
            PageResult<BookBO> boPageResult = bookDomainService.selectByPage(bookBO);
            PageResult<BookDTO> bookDTOPageResult = BookDtoConverter.INSTANCE.convertBoToDtoPage(boPageResult);
            return Result.ok(bookDTOPageResult);
        } catch (Exception e) {
            return Result.fail("分页查询图书失败");
        }
    }

    /**
     * 借书
     *
     * @param recordDTO
     * @return
     */
    @PostMapping("/borrow")
    public Result<Boolean> borrow(@RequestBody RecordDTO recordDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("BookController.borrow.dto:{}", JSON.toJSONString(recordDTO));
            }
            Preconditions.checkNotNull(recordDTO.getUserId(), "用户id不能为空");
            Preconditions.checkNotNull(recordDTO.getBookId(), "图书id不能为空");
            RecordBO recordBO = RecordDtoConverter.INSTANCE.convertDtoToRecordBo(recordDTO);
            return Result.ok(bookDomainService.borrow(recordBO));
        } catch (Exception e) {
            return Result.fail("借书失败");
        }
    }

    /**
     * 还书
     *
     * @param recordDTO
     * @return
     */
    @PostMapping("/returned")
    public Result<Boolean> returned(@RequestBody RecordDTO recordDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("BookController.returned.dto:{}", JSON.toJSONString(recordDTO));
            }
            Preconditions.checkNotNull(recordDTO.getId(), "记录id不能为空");
            RecordBO recordBO = RecordDtoConverter.INSTANCE.convertDtoToRecordBo(recordDTO);
            return Result.ok(bookDomainService.returned(recordBO));
        } catch (Exception e) {
            return Result.fail("还书失败");
        }
    }
}
